package com.web.service;

import com.web.VO.DemandAcceptDetailVO;
import com.web.VO.DemandAcceptStateVO;
import com.web.VO.MyDemandVO;
import com.web.dao.DemandAcceptMapper;
import com.web.dao.DemandInfoMapper;
import com.web.dao.NoticeMapper;
import com.web.entity.DemandAccept;
import com.web.entity.DemandInfo;
import com.web.entity.Notice;
import com.web.entity.UserInfo;
import com.web.enums.NoticeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author qh
 * @Date 2019-01-08
 * @description 需求service
 */
@Service
public class DemandService {

    @Resource
    private DemandAcceptMapper demandAcceptMapper;
    @Resource
    private DemandInfoMapper demandInfoMapper;
    @Resource
    private NoticeMapper noticeMapper;

    /**
     * 我要接单
     * @param info
     * @param demandId
     * @return
     */
    public int acceptDemand(UserInfo info, Integer demandId) {
        //1、保存接单信息
        DemandAccept demandAccept = new DemandAccept();
        demandAccept.setUserId(info.getUserId());
        demandAccept.setDemandId(demandId);
        Integer id = demandAcceptMapper.insertSelective(demandAccept);

        //2、发送通知
        try {
            Notice notice = new Notice();
            DemandInfo demandInfo = demandInfoMapper.selectByPrimaryKey(demandId);
            notice.setSender(info.getUserId());
            notice.setType(NoticeEnum.DEMAND_NOTICE.getCode());
            notice.setTarget(demandInfo.getUserId());
            notice.setTitle("需求接单");
            notice.setContent("您发布的"+demandInfo.getTitle()+"需求有人接单了");
            notice.setContentId(demandInfo.getId());
            noticeMapper.insertSelective(notice);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return id;
    }

    /**
     * 查询是否已经存在接单信息
     * @param demandAccept
     * @return
     */
    public DemandAccept getOne(DemandAccept demandAccept) {
        return demandAcceptMapper.selectIsExist(demandAccept);
    }

    /**
     * 查询我的接单
     * @param userId
     * @return
     */
    public List<MyDemandVO> listMyDemand(String userId) {
        return demandAcceptMapper.getMyDemandInfo(userId);
    }

    /**
     * 接单详情
     * @param demandId
     * @param userId
     * @return
     */
    public DemandAcceptDetailVO getDemandDetail(Integer demandId, String userId) {
        return demandAcceptMapper.getDemandDetail(demandId, userId);
    }

    /**
     * 接单情况
     * @param demandId
     * @return
     */
    public List<DemandAcceptStateVO> listDemandAcceptState(Integer demandId) {
        return demandAcceptMapper.listDemandAcceptState(demandId);
    }
}
